import { getRowNumber } from 'src/boot/grid';
import { api } from 'src/boot/api';
import { Row } from './models';
import { Condition } from 'src/utils/types';
import { storeToRefs } from 'pinia';
import { useTerminalStore } from '../store';

interface aa {
  code: number;
  message: string;
  data: Row[];
}

export const getData = async (conditions: Condition) => {
  return [{ orderNo: 'order111' }, { orderNo: 'order222' }];
  const { data } = await api.post<aa>('/OrderManager/queryOrder', conditions);
  const { code, message } = data;
  if (code === 200) {
    return data.data;
  } else {
    console.log(message);
  }
};

//指令状态 白色为空闲指令，绿色表示行车正在执行指令，橙色表示行车获取指令；红色表示校验失败
export const createGridOptions = () => ({
  getRowStyle: (params: any) => {
    if (params.data.exeCrane == '02') {
      return { backgroundColor: 'rgba(139, 0, 0,0.5)' };
    }

    if (params.data.orderStatus === 13) {
      return { backgroundColor: 'rgba(0, 100, 0,0.5)' };
    } else if (params.data.orderStatus === 11) {
      return { backgroundColor: 'rgba(184, 134, 11,0.5)' };
    } else {
      return { backgroundColor: 'rgba(0,0,0,0.05)' };
    }
  },
  rowSelection: 'multiple',
});

const getOrderStatus_ch = (params: any) => {
  if (params.data.orderStatus === 1) {
    return '初始化';
  } else if (params.data.orderStatus === 2) {
    return '激活';
  } else if (params.data.orderStatus === 11) {
    return '选中';
  } else if (params.data.orderStatus === 13) {
    return '起吊';
  } else if (params.data.orderStatus === 15) {
    return '卸下';
  } else {
    return '未知';
  }
};

const store = useTerminalStore();
const { orderTypeName } = storeToRefs(store);

const getorderType_ch = (params: any) => {
  return orderTypeName.value ? orderTypeName.value[params.data.orderType] : params.data.orderType;
  // if (params.data.orderType === 'AS4AS4') {
  //   return '倒垛';
  // } else if (params.data.orderType === 'RP0AS4') {
  //   return '机组下线';
  // } else if (params.data.orderType === 'CS4RP0') {
  //   return '机组上料';
  // } else if (params.data.orderType === 'T01AS4') {
  //   return '卡车入库';
  // }
  // else {
  //   return params.data.orderType;
  // }
};

export const createColumnDefs = () => [
  {
    headerName: '操作',
    field: 'ROW_NUMBER',
    width: 135,
    cellRenderer: 'ConfirmButton',
    // checkboxSelection: true,
    // headerCheckboxSelection: true,
    // headerCheckboxSelectionFilteredOnly: true,
    headerClass: 'custom-header',
    suppressMenu: true,
  },
  // { headerName: '指令号', field: 'orderNo', width: 125 },
  {
    headerName: '指令类型',
    field: 'orderType',
    width: 145,
    valueFormatter: getorderType_ch,
    headerClass: 'custom-header',
    suppressMenu: true,
  },
  { headerName: '上料顺', field: 'executeNumber', width: 85, headerClass: 'custom-header', suppressMenu: true },
  { headerName: '行车号', field: 'exeCrane', width: 85, headerClass: 'custom-header', suppressMenu: true },
  // {
  //   headerName: '指令状态',
  //   field: 'orderStatus',
  //   width: 125,
  //   valueFormatter: getOrderStatus_ch,
  // },
  { headerName: '材料号', field: 'matNo', width: 125, headerClass: 'custom-header', suppressMenu: true },
  { headerName: '起吊位', field: 'frName', width: 125, headerClass: 'custom-header', suppressMenu: true },
  { headerName: '卸下位', field: 'toName', width: 125, headerClass: 'custom-header', suppressMenu: true },

  { headerName: '下道机组', field: 'nextUnitNo', width: 125, headerClass: 'custom-header', suppressMenu: true },

  { headerName: '重量', field: 'netWeight', width: 105, headerClass: 'custom-header', suppressMenu: true },
  { headerName: '宽度', field: 'width', width: 105, headerClass: 'custom-header', suppressMenu: true },
];
